package com.zl.learning.thread.concurrent;

public class OutOfOrderDemo1 {

    private static int a = 0, b = 0, i = 0, j = 0;

    public static void main(String[] args) {
        int count = 0;

        while (true) {
            count++;
            a = 0;
            b = 0;
            i = 0;
            j = 0;
            Thread t1 = new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                a = 1;
                i = b;
            });
            Thread t2 = new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                b = 1;
                j = a;
            });
            t1.start();
            t2.start();
            try {
                t1.join();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("第%d次输出结果 i=%d,j=%d \n", count, i, j);
            if (i == 0 && j == 0) {
                break;
            }
        }
    }
}
